package o;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import o.dky.j;

public class cku extends DialogFragment {
    private static String ॱ = "BaseAlertDialogEx";
    protected int ʻ = 8;
    protected View ʼ;
    protected int ʽ = 0;
    public cks ˊ;
    private OnDismissListener ˊॱ = null;
    public ewa ˋ;
    protected CharSequence ˋॱ;
    protected CharSequence ˎ;
    protected CharSequence ˏ;
    protected CharSequence ˏॱ;
    private Map<Integer, c> ͺ = new HashMap();
    private ewe ॱˊ = null;
    private OnKeyListener ॱˎ;
    protected CharSequence ॱॱ;
    protected int ᐝ = 0;
    private OnCancelListener ᐝॱ;

    public static class c {
        private int ˋ;
        private int ॱ;

        public int ॱ() {
            return this.ˋ;
        }

        public void ˋ(int i) {
            this.ˋ = i;
        }

        public int ˊ() {
            return this.ॱ;
        }

        public void ˊ(int i) {
            this.ॱ = i;
        }
    }

    public static <T extends cku> cku ˋ(CharSequence charSequence, CharSequence charSequence2) {
        return ˏ(null, cku.class, charSequence, charSequence2);
    }

    public static <T extends cku> cku ˏ(Context context, Class<T> cls, CharSequence charSequence, CharSequence charSequence2) {
        try {
            cku o_cku = (cku) cls.newInstance();
            Bundle bundle = new Bundle();
            bundle.putCharSequence("tileContent", charSequence);
            bundle.putCharSequence("content", charSequence2);
            o_cku.setArguments(bundle);
            return o_cku;
        } catch (InstantiationException e) {
            return new cku();
        } catch (InstantiationException e2) {
            return new cku();
        } catch (IllegalAccessException e3) {
            return new cku();
        }
    }

    public static boolean ˏ(Context context, String str) {
        if (context == null || !(context instanceof Activity) || ((Activity) context).getFragmentManager().findFragmentByTag(str) == null) {
            return false;
        }
        return true;
    }

    public static void ˎ(Context context, String str) {
        if (context != null) {
            try {
                if (context instanceof Activity) {
                    Fragment findFragmentByTag = ((Activity) context).getFragmentManager().findFragmentByTag(str);
                    if (findFragmentByTag != null && (findFragmentByTag instanceof cku)) {
                        try {
                            ((cku) findFragmentByTag).dismissAllowingStateLoss();
                        } catch (IllegalStateException e) {
                            Logger.e(ॱ, e.toString());
                        }
                    }
                }
            } catch (Throwable e2) {
                Logger.ˋ(ॱ, "BaseAlertDialogEx dismiss Exception:", e2);
            }
        }
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setRetainInstance(true);
    }

    public void ˎ(Context context) {
        ˋ(context, getClass().getSimpleName());
    }

    public void ˋ(Context context, String str) {
        if (context == null || !(context instanceof Activity) || isAdded()) {
            Logger.e(ॱ, "context instance type isn't FragmentActivity, instance:" + context + ",isAdded: " + isAdded());
            return;
        }
        Activity activity = (Activity) context;
        if (!ActivityUtil.isAcitivtyNotValid(activity)) {
            FragmentTransaction beginTransaction = activity.getFragmentManager().beginTransaction();
            Fragment findFragmentByTag = activity.getFragmentManager().findFragmentByTag(str);
            if (!(findFragmentByTag == null || findFragmentByTag.isAdded())) {
                beginTransaction.remove(findFragmentByTag);
            }
            beginTransaction.addToBackStack(null);
            try {
                show(beginTransaction, str);
            } catch (IllegalStateException e) {
                Logger.e(ॱ, "show dialog error " + e.toString());
            }
        }
    }

    public void ॱ(Context context, String str) {
        if (context == null || !(context instanceof Activity)) {
            Logger.e(ॱ, "context instance type isn't Activity, instance:" + context);
            return;
        }
        Activity activity = (Activity) context;
        if (!ActivityUtil.isAcitivtyNotValid(activity)) {
            FragmentTransaction beginTransaction = activity.getFragmentManager().beginTransaction();
            Fragment findFragmentByTag = activity.getFragmentManager().findFragmentByTag(str);
            if (findFragmentByTag != null) {
                beginTransaction.remove(findFragmentByTag);
                beginTransaction.addToBackStack(null);
                return;
            }
            beginTransaction.add(this, str);
            beginTransaction.commitAllowingStateLoss();
        }
    }

    public Dialog onCreateDialog(Bundle bundle) {
        Dialog create = ˋ(getActivity()).create();
        ayw.ˊ(create.getWindow());
        return create;
    }

    protected Builder ˋ(Context context) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            this.ˏॱ = arguments.getCharSequence("tileContent");
            this.ˋॱ = arguments.getCharSequence("content");
        }
        Builder builder = new Builder(context);
        builder.setTitle(this.ˏॱ);
        if (TextUtils.isEmpty(this.ˋॱ)) {
            builder.setMessage(null);
        } else {
            builder.setMessage(this.ˋॱ);
        }
        if (this.ᐝ == 0) {
            builder.setPositiveButton(j.ˊॱ, new OnClickListener(this) {
                final /* synthetic */ cku ˏ;

                {
                    this.ˏ = r1;
                }

                public void onClick(DialogInterface dialogInterface, int i) {
                    if (!ActivityUtil.isAcitivtyNotValid(this.ˏ.getActivity())) {
                        this.ˏ.ˊ(dialogInterface);
                        try {
                            this.ˏ.dismissAllowingStateLoss();
                        } catch (IllegalStateException e) {
                            Logger.e(cku.ॱ, e.toString());
                        }
                    }
                }
            });
        }
        if (this.ʽ == 0) {
            builder.setNegativeButton(j.ͺ, new OnClickListener(this) {
                final /* synthetic */ cku ˎ;

                {
                    this.ˎ = r1;
                }

                public void onClick(DialogInterface dialogInterface, int i) {
                    if (!ActivityUtil.isAcitivtyNotValid(this.ˎ.getActivity())) {
                        this.ˎ.ˋ(dialogInterface);
                        try {
                            this.ˎ.dismissAllowingStateLoss();
                        } catch (IllegalStateException e) {
                            Logger.e(cku.ॱ, e.toString());
                        }
                    }
                }
            });
        }
        if (this.ʻ == 0) {
            builder.setNeutralButton(this.ॱॱ, new OnClickListener(this) {
                final /* synthetic */ cku ˎ;

                {
                    this.ˎ = r1;
                }

                public void onClick(DialogInterface dialogInterface, int i) {
                    if (!ActivityUtil.isAcitivtyNotValid(this.ˎ.getActivity())) {
                        this.ˎ.ˎ(dialogInterface);
                        try {
                            this.ˎ.dismissAllowingStateLoss();
                        } catch (IllegalStateException e) {
                            Logger.e(cku.ॱ, e.toString());
                        }
                    }
                }
            });
        }
        if (this.ʼ != null) {
            builder.setView(this.ʼ);
        }
        return builder;
    }

    private void ˊ(DialogInterface dialogInterface) {
        if (this.ˊ != null) {
            this.ˊ.enableNetworking();
        }
        if (this.ˋ != null) {
            this.ˋ.ˏ(getActivity(), dialogInterface, -1);
        }
    }

    private void ˋ(DialogInterface dialogInterface) {
        if (this.ˊ != null) {
            this.ˊ.upgrade();
        }
        if (this.ˋ != null) {
            this.ˋ.ˏ(getActivity(), dialogInterface, -2);
        }
    }

    private void ˎ(DialogInterface dialogInterface) {
        if (this.ˊ != null) {
            this.ˊ.ˋ();
        }
        if (this.ˋ != null) {
            this.ˋ.ˏ(getActivity(), dialogInterface, -3);
        }
    }

    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        ewb.ˊ(dialog);
        ayw.ˊ(dialog == null ? null : dialog.getWindow());
        ˊ(dialog);
        ˏ(dialog);
        if (dialog != null && this.ॱˎ != null) {
            dialog.setOnKeyListener(this.ॱˎ);
        }
    }

    private void ˏ(Dialog dialog) {
        if (this.ͺ.size() > 0) {
            if (dialog == null || !(dialog instanceof AlertDialog)) {
                dialog = null;
            } else {
                dialog = (AlertDialog) dialog;
            }
            if (dialog != null) {
                for (Entry entry : this.ͺ.entrySet()) {
                    int intValue = ((Integer) entry.getKey()).intValue();
                    c cVar = (c) entry.getValue();
                    if (cVar != null) {
                        Button button = dialog.getButton(intValue);
                        if (button != null) {
                            if (cVar.ˊ() != 0) {
                                button.setTextColor(cVar.ˊ());
                            }
                            if (cVar.ॱ() != 0) {
                                button.setBackgroundResource(cVar.ॱ());
                            }
                        }
                    }
                }
            }
        }
    }

    private void ˊ(Dialog dialog) {
        if (dialog == null || !(dialog instanceof AlertDialog)) {
            dialog = null;
        } else {
            dialog = (AlertDialog) dialog;
        }
        if (dialog != null) {
            Button button = dialog.getButton(-1);
            if (!(button == null || TextUtils.isEmpty(this.ˏ))) {
                button.setText(this.ˏ);
                ewb.ˏ(ContentManager.getInstant().getContext(), button, this.ˏ.toString());
            }
            button = dialog.getButton(-2);
            if (!(button == null || TextUtils.isEmpty(this.ˎ))) {
                button.setText(this.ˎ);
                ewb.ˏ(ContentManager.getInstant().getContext(), button, this.ˎ.toString());
            }
            button = dialog.getButton(-3);
            if (button != null && !TextUtils.isEmpty(this.ॱॱ)) {
                button.setText(this.ॱॱ);
                ewb.ˏ(ContentManager.getInstant().getContext(), button, this.ॱॱ.toString());
            }
        }
    }

    public void ˊ(View view) {
        this.ʼ = view;
    }

    public void ॱ(int i, CharSequence charSequence) {
        if (i == -1) {
            this.ˏ = charSequence;
        } else if (i == -2) {
            this.ˎ = charSequence;
        } else if (i == -3) {
            this.ॱॱ = charSequence;
        }
    }

    public void ˎ(int i, int i2) {
        if (i == -1) {
            this.ᐝ = i2;
        } else if (i == -2) {
            this.ʽ = i2;
        } else if (i == -3) {
            this.ʻ = i2;
        }
    }

    public void onDestroyView() {
        Dialog dialog = getDialog();
        if (dialog != null && getRetainInstance()) {
            dialog.setDismissMessage(null);
        }
        super.onDestroyView();
        if (this.ʼ != null) {
            ViewGroup viewGroup = (ViewGroup) this.ʼ.getParent();
            if (viewGroup != null) {
                viewGroup.removeView(this.ʼ);
            }
        }
    }

    public void onCancel(DialogInterface dialogInterface) {
        if (this.ॱˊ != null) {
            this.ॱˊ.ॱ();
        }
        if (this.ᐝॱ != null) {
            this.ᐝॱ.onCancel(dialogInterface);
        }
        if (this.ˋ != null) {
            this.ˋ.ˊ(getActivity(), dialogInterface);
        }
        super.onCancel(dialogInterface);
    }

    public void onDismiss(DialogInterface dialogInterface) {
        try {
            super.onDismiss(dialogInterface);
            if (this.ˊॱ != null) {
                this.ˊॱ.onDismiss(dialogInterface);
            }
        } catch (Throwable e) {
            Logger.ˋ(ॱ, "onDismiss error", e);
        }
    }

    public void ॱ(ewe o_ewe) {
        this.ॱˊ = o_ewe;
    }

    public void ˎ(OnDismissListener onDismissListener) {
        this.ˊॱ = onDismissListener;
    }

    public void ॱ(cks o_cks) {
        this.ˊ = o_cks;
    }

    public void ˋ(OnCancelListener onCancelListener) {
        this.ᐝॱ = onCancelListener;
    }

    public void ˏ(OnKeyListener onKeyListener) {
        this.ॱˎ = onKeyListener;
    }

    public void ॱ(int i, c cVar) {
        this.ͺ.put(Integer.valueOf(i), cVar);
    }

    public void ˋ(ewa o_ewa) {
        this.ˋ = o_ewa;
    }
}
